package com.yingyi.org.controller;

import com.yingyi.basic.annotation.PreAuthorize;
import com.yingyi.basic.exception.ParentDepartmentException;
import com.yingyi.basic.util.JsonResult;
import com.yingyi.basic.util.PageList;
import com.yingyi.org.domain.Department;
import com.yingyi.org.query.DepartmentQuery;
import com.yingyi.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "部门的API",tags="部门相关的CRUD功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;
    /**
     * restful风格的请求
     * 使用动作+url来确定请求
     * url：定位资源
     * 动作：具体要做什么
     */


    /**
     *  url: http://localhost:8080/department
     * 查询所有
     * @RequestMapping(value = "", method = RequestMethed.Get)
     */
    @PreAuthorize(name = "获取所有部门", sn = "department:queryAll")
    @ApiOperation(value = "查询所有部门数据" )
    @GetMapping
    public List<Department> queryAll(){
        try {
            return departmentService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Department>();
        }
    }


    /**
     *  url: http://localhost:8080/department/deptTree
     * 查询部门树
     * @RequestMapping(value = "", method = RequestMethed.Get)
     */
    @PreAuthorize(name = "查询部门树", sn = "department:deptTree")
    @ApiOperation(value = "查询部门树信息" )
    @GetMapping("/deptTree")
    public List<Department> getDeptTree(){
        try {
            return departmentService.getDeptTree();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Department>();
        }
    }











    /**
     * 根据id查询
     * url: http://localhost:8080/department
     * method: Get
      */
    @PreAuthorize(name = "查询部门", sn = "department:queryById")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            //@ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="form"),
            //@ApiImplicitParam(name="password",value="密码",required=true,paramType="form"),
            @ApiImplicitParam(name="id", value="编号", required = true, paramType = "path", dataType = "Long")
    })
    @GetMapping("/{id}")
    public Department queryById(@PathVariable Long id){
        try {
            return departmentService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 增加或修改
     * url: http://localhost:8080/department
     * @RequestBody 从请求体中获取数据
     */
    //@ApiParam(name="待操作的Department对象，id为null表示添加，id非null表示修改",required = true)
    @PreAuthorize(name = "保存部门", sn = "department:save")
    @ApiOperation(value = "增加或修改部门信息" )
    @ApiImplicitParams({
            //@ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="form"),
            //@ApiImplicitParam(name="password",value="密码",required=true,paramType="form"),
            @ApiImplicitParam(name="department",  value="待操作的Department对象，id为null表示添加，id非null表示修改",required = true,
                    paramType = "query", dataType = "com.yingyi.org.domain.Department"
            )
    })
    @PostMapping
    public JsonResult save(@RequestBody Department department){
        try {
            if(department.getId()==null){
                //添加操作
                departmentService.insert(department);
            }else{
                //修改操作
                //当前部门不能选择自己本身作为上级部门
                if(department.getId().equals(department.getParent_id())){
                    System.out.println("in update exception");
                    throw new ParentDepartmentException("不能选择本部门作为父部门");
                }
                departmentService.update(department);
            }
            System.out.println("in save >>>>>>>>");
            return JsonResult.me();
        } catch (ParentDepartmentException e){
            e.printStackTrace();
            return JsonResult.me().setMessage(e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("操作失败");
        }
    }

    /**
     * 删除操作
     * url: http://localhost:8080/department/1
     * 操作 Delete
     * @ApiParam(name="ID属性",required = true)
     */
    @PreAuthorize(name = "删除部门", sn = "department:delete")
    @ApiOperation(value = "通过ID删除部门数据" )
    @ApiImplicitParams({
            //@ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="form"),
            //@ApiImplicitParam(name="password",value="密码",required=true,paramType="form"),
            @ApiImplicitParam(name="id", value="ID属性",required = true, paramType = "path",
            dataType = "Long")
    })
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable Long id){
        try {
            departmentService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     */

    @PreAuthorize(name = "批量删除部门", sn = "department:patchDelete")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="待删除的ID集合",required = true, paramType = "query",
                    dataType = "java.util.ArrayList")
    })
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("删除失败，请稍后重试！");
        }
    }




    /**
     * 分页
     * url http://localhost:8080/department/
     * 方式 Put
     * @ApiParam(name="查询的参数",required = true)
     */
    @PreAuthorize(name = "分页查询部门", sn = "department:pageQuery")
    @ApiOperation(value = "分页查询和条件查询" )
    @ApiImplicitParams({
            //@ApiImplicitParam(name="mobile",value="手机号",required=true,paramType="form"),
            //@ApiImplicitParam(name="password",value="密码",required=true,paramType="form"),
            @ApiImplicitParam(name="query", value="查询的参数",required = true,
                    paramType = "query", dataType = "com.yingyi.org.query.DepartmentQuery"
            )
    })
    @PutMapping
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery query){
        try {
            return departmentService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Department>();
        }
    }


}
